package CS4310.csueb.visual;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.Timer;

///////////////////////////// drawHeroSprite /////////////////////////////
//performs an action on the keyboard input for heroSprite class and enables
//the hero sprite to "move"
public class DrawHeroSprite extends JLabel implements ActionListener {

	private static final long serialVersionUID = -1955282947156310909L;
	private static HeroSprite hsprite;
	private Timer timer;

	// /////////////////// inner class spriteListener //////////////
	private class spriteListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			hsprite.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			hsprite.keyPressed(e);
		}
	}// ///// end class spriteListener

	// *********** constructor *************//
	public DrawHeroSprite(String pfilename, int px, int py) {
		setDoubleBuffered(true);
		setLayout(null); // custom layout
		addKeyListener(new spriteListener());
		setFocusable(true); // used to bring focus to this panel for key
							// listener
		setFocusTraversalKeysEnabled(true); // will listen to keys to set focus
											// (ex tab)

		applySpriteImage(pfilename, px, py);
		setBounds(0, 0, 800, 580);

		timer = new Timer(5, this);
		timer.start();
	}

	// ********* applySpriteImage ***************//
	private void applySpriteImage(String pfilename, int x, int y) {
		hsprite = new HeroSprite(pfilename, x, y);
	}

	// ********** actionPerform **************/
	public void actionPerformed(ActionEvent e) {
		hsprite.move();
		repaint();
	}

	// ********* override paintComponent() *********//
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;

		// draws the whole image "img" at accessed coordinates
		g2d.drawImage(hsprite.getImg(), hsprite.getx(), hsprite.gety(), this);
	}

}
